package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.CarriageDao;
import com.my12306.train.model.entity.CarriageEntity;
import com.my12306.train.service.CarriageService;
import com.my12306.user.model.vo.CarriageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("carriageService")
public class CarriageServiceImpl extends ServiceImpl<CarriageDao, CarriageEntity> implements CarriageService {
    @Resource
    CarriageDao carriageDao;

    public PageUtils find(CarriageVO carriageVO, long page, long size) {
        Page<CarriageEntity> page1 = new Page<>(page, size);
        QueryWrapper<CarriageEntity> qs = new QueryWrapper<>();

        if (carriageVO == null) {
            return new PageUtils(carriageDao.selectPage(page1, qs));
        }

        if (carriageVO.getCoachNumber() != null) {
            qs.like("coach_number", carriageVO.getCoachNumber());
        }
        if (carriageVO.getCoachSitType() != null) {
            qs.like("coach_sit_type", carriageVO.getCoachSitType());
        }
        if (carriageVO.getCoachType() != null) {
            qs.like("coach_type", carriageVO.getCoachType());
        }

        return new PageUtils(carriageDao.selectPage(page1, qs));
    }

    @Override
    public PageUtils queryCarriagePage(PageVo pageVo) {
        pageVo = pageVo.init();
        List<CarriageEntity> carriages = this.baseMapper.queryCarriagePage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        if(carriages != null){
            Integer dataCount = queryCarriageCount(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
            System.out.println("数据总量：" + dataCount);
            return PageUtils.build(pageVo, carriages, dataCount);
        }
        return null;
    }

    @Override
    public Integer queryCarriageCount(PageVo pageVo, int logicDelete) {
        Integer count = this.baseMapper.queryCarriageCount(pageVo, logicDelete);
        return count;
    }

    @Override
    @Transactional
    public ResponseData deleteCarriageBatchByIds(List<Long> carriageIds) {
        this.baseMapper.deleteCarriageBatchByIds(carriageIds, CommonConstants.LogicDelete.delete.getValue());
        return ResponseData.ok();
    }

    @Override
    public Long saveCarriage(CarriageEntity carriage) {
        if(carriage != null){
            carriage.setCarriageId(SnowflakeIdWorker.generateId());
            carriage.setCreateDate(new Date());
            carriage.setUpdateDate(new Date());
            int insert = this.baseMapper.insert(carriage);
            System.out.println("成功添加车厢数：" + insert);
        }
        return null;
    }

    @Override
    public void updateCarriageById(CarriageEntity carriage) {
        if(carriage != null){
            carriage.setUpdateDate(new Date());
            this.baseMapper.updateById(carriage);
        }
    }

    @Override
    public List<CarriageEntity> queryCarriageNoMar() {
        QueryWrapper<CarriageEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
        queryWrapper.eq("coach_number",0);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<CarriageEntity> queryAllCarriageByTrainCode(String trainCode, Integer logicDelete) {
        if (trainCode!=null&&logicDelete!=null){
        return  this.baseMapper.queryAllCarriageByTrainCode(trainCode,logicDelete);
        }
        return null;
    }
}
